package com.ruoyi.buss.report.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.druid.util.StringUtils;
import com.ruoyi.buss.utils.DateUtilss;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.buss.report.mapper.ReportMarketJxxtLsbMapper;
import com.ruoyi.buss.report.domain.ReportMarketJxxtLsb;
import com.ruoyi.buss.report.service.IReportMarketJxxtLsbService;

/**
 * 营销业绩统计历史，每天保存4.1.2Service业务层处理
 * 
 * @author sxy
 * @date 2021-07-16
 */
@Service
public class ReportMarketJxxtLsbServiceImpl implements IReportMarketJxxtLsbService 
{
    @Autowired
    private ReportMarketJxxtLsbMapper reportMarketJxxtLsbMapper;

    /**
     * 查询营销业绩统计历史，每天保存4.1.2
     * 
     * @param sjrq 营销业绩统计历史，每天保存4.1.2ID
     * @return 营销业绩统计历史，每天保存4.1.2
     */
    @Override
    public ReportMarketJxxtLsb selectReportMarketJxxtLsbById(String sjrq)
    {
        return reportMarketJxxtLsbMapper.selectReportMarketJxxtLsbById(sjrq);
    }

    /**
     * 查询营销业绩统计历史，每天保存4.1.2列表
     * 
     * @param reportMarketJxxtLsb 营销业绩统计历史，每天保存4.1.2
     * @return 营销业绩统计历史，每天保存4.1.2
     */
    @Override
    public List<ReportMarketJxxtLsb> selectReportMarketJxxtLsbList(ReportMarketJxxtLsb reportMarketJxxtLsb, LoginUser userInfo,int type)
    {
        String orgId = userInfo.getSysUser().getDept().getOrgId();
        String userId = userInfo.getSysUser().getUserName();
        //如果是admin用户不做处理
        if(userInfo.getRoles().contains("zhhz")){
            reportMarketJxxtLsb.setOrgId(orgId);
        } else if(userInfo.getRoles().contains("common")){
            reportMarketJxxtLsb.setOrgId(orgId);
            reportMarketJxxtLsb.setUserId(userId);
        }

      /*  if (StringUtils.isEmpty(reportMarketJxxtLsb.getSjrq())) {
            // 获取去年最后一天
            reportMarketJxxtLsb.setSjrq(DateUtilss.getLastYear(new Date(), -1));
        }else {
            reportMarketJxxtLsb.setSjrq(reportMarketJxxtLsb.getSjrq().replace("-", ""));
        }*/
        // 获取上个月最后一天
       /* reportMarketJxxtLsb
                .setMonthEnd(reportMarketJxxtLsb.getSjrq() == null || reportMarketJxxtLsb.getSjrq().length()<8?
                        DateUtilss.getLastDayOfMonth(Integer.valueOf(DateUtils.getMMddStr().substring(0, 2))-1):
                        Integer.valueOf(reportMarketJxxtLsb.getSjrq().substring(4,6)) == 1 ?
                                Integer.valueOf(reportMarketJxxtLsb.getSjrq().substring(0,4))-1+"1231":
                                reportMarketJxxtLsb.getSjrq().substring(0,4)+ DateUtilss.getLastDayOfMonths(Integer.valueOf(reportMarketJxxtLsb.getSjrq().substring(4,6))-1));*/
        List<ReportMarketJxxtLsb> doList = new ArrayList<ReportMarketJxxtLsb>();

        // 0表示进度表1表示趋势图 3表示导出
        if (type == 0) {
            doList = reportMarketJxxtLsbMapper.yxyjtjb(reportMarketJxxtLsb);
            if (doList.size() > 0) {
                ReportMarketJxxtLsb vo = reportMarketJxxtLsbMapper.yxyjtjbhz(reportMarketJxxtLsb).get(0);
                vo.setOrgName("总行汇总数据");
                doList.add(vo);
            }
        } else if (type == 1) {
            doList = reportMarketJxxtLsbMapper.yxyjtjbhz(reportMarketJxxtLsb);
        } else if (type == 3) {
            doList = reportMarketJxxtLsbMapper.yxyjtjb(reportMarketJxxtLsb);
        }


        return reportMarketJxxtLsbMapper.selectReportMarketJxxtLsbList(reportMarketJxxtLsb);
    }

    /**
     * 新增营销业绩统计历史，每天保存4.1.2
     * 
     * @param reportMarketJxxtLsb 营销业绩统计历史，每天保存4.1.2
     * @return 结果
     */
    @Override
    public int insertReportMarketJxxtLsb(ReportMarketJxxtLsb reportMarketJxxtLsb)
    {
        return reportMarketJxxtLsbMapper.insertReportMarketJxxtLsb(reportMarketJxxtLsb);
    }

    /**
     * 修改营销业绩统计历史，每天保存4.1.2
     * 
     * @param reportMarketJxxtLsb 营销业绩统计历史，每天保存4.1.2
     * @return 结果
     */
    @Override
    public int updateReportMarketJxxtLsb(ReportMarketJxxtLsb reportMarketJxxtLsb)
    {
        return reportMarketJxxtLsbMapper.updateReportMarketJxxtLsb(reportMarketJxxtLsb);
    }

    /**
     * 批量删除营销业绩统计历史，每天保存4.1.2
     * 
     * @param sjrqs 需要删除的营销业绩统计历史，每天保存4.1.2ID
     * @return 结果
     */
    @Override
    public int deleteReportMarketJxxtLsbByIds(String[] sjrqs)
    {
        return reportMarketJxxtLsbMapper.deleteReportMarketJxxtLsbByIds(sjrqs);
    }

    /**
     * 删除营销业绩统计历史，每天保存4.1.2信息
     * 
     * @param sjrq 营销业绩统计历史，每天保存4.1.2ID
     * @return 结果
     */
    @Override
    public int deleteReportMarketJxxtLsbById(String sjrq)
    {
        return reportMarketJxxtLsbMapper.deleteReportMarketJxxtLsbById(sjrq);
    }
}
